package org.xhy.hrm.asserts;

import org.springframework.util.StringUtils;
import org.xhy.hrm.globle.GlobleException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GlobleAssert {

    //手机号的正则表达式
    public static final String REGEX_PHONE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
    //邮箱正则表达式
    public static final String REGEX_EMAIL = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";

    //断言，字符串不为空
    public static void hasText(String text,String message){
        if (!StringUtils.hasText(text))
            throw new GlobleException(message);
    }

    //断言，手机号正则
    //Pattern专门做正则的一个类，Matcher就是匹配
    public static void isPhone(String phone,String message){
        Pattern p = Pattern.compile(REGEX_PHONE, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(phone);
        if (!m.matches())
            throw new GlobleException(message);
    }

    //断言，邮箱正则
    public static void isEmail(String email,String message){
        Pattern p = Pattern.compile(REGEX_EMAIL, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(email);
        if (!m.matches())
            throw new GlobleException(message);
    }

    /**
     * 断言，对象不嫩为空
     * @param obj
     * @param message
     */
    public static void isNotNull(Object obj, String message) {
        if (obj == null){
            throw new GlobleException(message);
        }
    }

    /**
     * 断言,两个值相等
     * @param i1
     * @param i2
     * @param message
     */
    public static void isEquals(int i1, int i2, String message) {
        if (i1 != i2){
            throw new GlobleException(message);
        }
    }

    /**
     * 断言，两个字符串相等
     * @param i1
     * @param i2
     * @param message
     */
    public static void isEquals(String i1, String i2, String message) {
        if (!i1.equalsIgnoreCase(i2)){
            throw new GlobleException(message);
        }
    }

    /**
     *
     * @param success
     * @param message
     */
    public static void isTrue(boolean success, String message) {
        if (!success){
            throw new GlobleException(message);
        }
    }

    public static void isFalse(Boolean hasKey, String message) {
        isTrue(!hasKey,message);
    }
}
